<?php
namespace App\Http\Middleware;

use Closure;
use Auth;

class WechatAuth
{
    protected $appid = 'wx456c24d2ccc433c7';
    protected $appsecret = '5b7281289ff05dfbcc49d30134aa30c5';

	/**
	 * Handle an incoming request.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  \Closure  $next
	 * @return mixed
	 */
	public function handle($request, Closure $next)
	{
        if (Auth::client()->guest())
        {
            if ($request->ajax() || $request->wantsJson())
            {
                return response()->json([
                    'status' => 401,
                    'message' => 'Unauthorized',
                    'data' => (object) null
                ]);
            }
            else
            {
                $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=".urlencode(route('WechatHome'))."&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
                if(!$request->has('token')){
                    //301跳转m端
                    header('HTTP/1.1 301 Moved Permanently');
                    header('Location: '.$url);
                    die;
                }else{
                    // 微信登录

                }
            }
        }

		return $next($request);
	}
}
